﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Resto.CostControl.WebControl.DWZAdapter;
using Enterprise.Framework.DomainModel;
using Enterprise.Framework.DomainModel.Specifications;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Query.DishModule.QueryObject;
using Resto.CostControl.Core.Query.DishModule.ViewObject;
using Resto.CostControl.Core.Facade.DishModule.DataTransferObject;
using Resto.CostControl.Core.Facade.DishModule.FacadeObject;
using Enterprise.Framework.Core.Exceptions;
using Resto.CostControl.Core;
using Resto.CostControl.WebControl.Service;

namespace Resto.CostControl.Web.Controllers
{
    public class DishController : Controller
    {

        public ActionResult Index(DWZPageInfo pageInfo)
        {
            DishQO query = ObjectCreator.Create<DishQO>();
            PagerCondition pager = pageInfo.ToPager();

            ISpecification<DishVO> spec = null;
            if (!string.IsNullOrWhiteSpace(pageInfo.Keywords))
            {
                spec = Specification<DishVO>.Create(p => p.Name == pageInfo.Keywords);
            }

            DishVO[] array = query.QueryDishVOByPager(spec, pager, "Code", true);

            ViewBag.Total = pager.RecordCount;
            ViewBag.PageNum = pageInfo.PageIndex;
            ViewBag.NumPerPage = pageInfo.PageSize;
            ViewBag.OrderField = "Code";
            ViewBag.OrderDirection = "asc";

            return View(array);
        }

        public ActionResult Edit(string Id)
        {
            DishDTO data = null;
            if (Id != null)
            {
                DishFO facade = ObjectCreator.Create<DishFO>();

                data = facade.GetDishDTO(Id);
            }
            else
            {
                data = new DishDTO();
            }
            return View(data);
        }

        [HttpPost]
        public JsonResult Save(DishDTO data, List<DishMaterialDTO> items)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (items == null)
                    {
                        items = new List<DishMaterialDTO>();
                    }
                    DishFO facade = ObjectCreator.Create<DishFO>();
                    if (!string.IsNullOrWhiteSpace(data.ID))
                        facade.UpdateDish(data, items);
                    else
                        facade.AddDish(data, items);

                }
                catch (BusinessValidationException ex)
                {
                    throw ex;
                }
            }

            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "Dish",
                callbackType = "closeCurrent",
                forwardUrl = "",
            };
            return Json(result);
        }



        [HttpPost]
        public JsonResult Delete(string id)
        {
            DishFO facade = ObjectCreator.Create<DishFO>();
            facade.DeleteDish(id);

            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "Dish",
                callbackType = "",
                forwardUrl = "",
            };
            return Json(result);
        }

        [HttpPost]
        public JsonResult Deletes(string[] Ids)
        {
            DishFO facade = ObjectCreator.Create<DishFO>();
            foreach (var id in Ids)
            {
                facade.DeleteDish(id);
            }
            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "Dish",
                callbackType = "",
                forwardUrl = "",
            };
            return Json(result);
        }

        public ActionResult LookupDish(DWZPageInfo pageInfo)
        {
            DishQO query = ObjectCreator.Create<DishQO>();
            PagerCondition pager = pageInfo.ToPager();

            ISpecification<DishVO> spec = null;
            if (!string.IsNullOrWhiteSpace(pageInfo.Keywords))
            {
                spec = Specification<DishVO>.Create(p => p.Name == pageInfo.Keywords);
            }

            DishVO[] array = query.QueryDishVOByPager(spec, pager, "Code", true);

            ViewBag.Total = pager.RecordCount;
            ViewBag.PageNum = pageInfo.PageIndex;
            ViewBag.NumPerPage = pageInfo.PageSize;
            ViewBag.OrderField = "Code";
            ViewBag.OrderDirection = "asc";

            return View(array);
        }

        [HttpPost]
        public JsonResult DeleteItem(string Id)
        {
            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "Dish",
                callbackType = "closeCurrent",
                forwardUrl = "",
            };
            return Json(result);
        }
    }
}